"use strict";
cc._RF.push(module, '1b495ZgJMJCRKbDFPrtl2Pb', 'Web_XHR');
// Script/Util/Network/XHR/Web_XHR.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Web_XHR = void 0;
class Web_XHR {
    /**
     * get json
     * @param url 地址
     * @param success 成功执行
     * @param fail  失败执行
     */
    static getJSON(url, success, fail) {
        var xhr = new XMLHttpRequest();
        xhr.responseType = 'json';
        xhr.open('GET', url, true);
        xhr.onload = function () {
            if (xhr.status >= 200 && xhr.status < 300) {
                if (success)
                    success(xhr.response);
            }
            else {
                if (fail)
                    fail(xhr.statusText);
                console.error(xhr.statusText, xhr.status);
            }
        };
        xhr.onerror = (ev) => {
            console.error(`Error from HTTP request url=:${url} ${xhr.status}: ${xhr.statusText}`);
            fail(ev);
        };
        xhr.ontimeout = (ev) => {
            console.error(`Timeout from HTTP request.`);
            fail(ev);
        };
        xhr.send();
    }
    /**
      * get json
      * @param url 地址
      * @param success 成功执行
      * @param fail  失败执行
      */
    static post(url, data, success, fail) {
        var xhr = new XMLHttpRequest();
        xhr.responseType = 'json';
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.open('post', url, true);
        xhr.onload = function () {
            if (xhr.status >= 200 && xhr.status < 300) {
                if (success)
                    success(xhr.response);
            }
            else {
                if (fail)
                    fail(xhr.statusText);
                console.error(xhr.statusText, xhr.status);
            }
        };
        xhr.onerror = (ev) => {
            console.error(`Error from HTTP request url=:${url} ${xhr.status}: ${xhr.statusText}`);
            fail(ev);
        };
        xhr.ontimeout = (ev) => {
            console.error(`Timeout from HTTP request.`);
            fail(ev);
        };
        xhr.send(JSON.stringify(data));
    }
    static post_Async(url, data) {
        return new Promise((resolve, reject) => {
            var xhr = new XMLHttpRequest();
            xhr.responseType = 'json';
            xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            xhr.open('post', url, true);
            xhr.onload = function () {
                if (xhr.status >= 200 && xhr.status < 300) {
                    if (resolve)
                        resolve(xhr.response);
                }
                else {
                    if (reject)
                        reject(xhr.statusText);
                    console.error(xhr.statusText, xhr.status);
                }
            };
            xhr.onerror = (ev) => {
                console.error(`Error from HTTP request url=:${url} ${xhr.status}: ${xhr.statusText}`);
                reject(ev);
            };
            xhr.ontimeout = (ev) => {
                console.error(`Timeout from HTTP request.`);
                reject(ev);
            };
            xhr.send(JSON.stringify(data));
        });
    }
    /**
     *  get json 使用 Promise 方式
     * @param url
     */
    static getJSON_Async(url) {
        return new Promise((resolve, reject) => {
            var xhr = new XMLHttpRequest();
            xhr.responseType = 'text';
            xhr.open('GET', url, true);
            xhr.onload = () => {
                if (xhr.status >= 200 && xhr.status < 300) {
                    resolve(xhr.responseText);
                }
                else {
                    console.error(xhr.statusText, xhr.status);
                    reject(xhr);
                }
            };
            xhr.onerror = () => {
                console.log(`Error from HTTP request. ${xhr.status}: ${xhr.statusText}`);
                reject(xhr);
            };
            xhr.ontimeout = () => {
                console.log(`Timeout from HTTP request.`);
                reject(xhr);
            };
            xhr.send();
        });
    }
}
exports.Web_XHR = Web_XHR;

cc._RF.pop();